package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.media.dto.SensitiveDto;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-03
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    /**
     * 敏感词分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult querByPage(SensitiveDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //构建分页对象
        IPage<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //构建条件查询对象
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(dto.getName())){
            queryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        this.page(page,queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal(),page.getRecords());

        return responseResult;
    }


    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveWmSensitive(WmSensitive wmSensitive) {
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive one = this.getOne(queryWrapper);
        if (one!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        this.save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }


    /**
     * 编辑敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateWmSensitive(WmSensitive wmSensitive) {
//        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(WmSensitive::getId,wmSensitive.getId());
//        WmSensitive one = this.getOne(queryWrapper);
//        if (one!=null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
//        }

        this.updateById(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }
}
